if(typeof giant === 'undefined')
    var giant = {};

if(typeof giant.ui === 'undefined')
/** @namespace Giant UI 库核心名称空间。*/
giant.ui = {};

(function($) {
	/**
	 * 创建一个页面操作提示控件。
	 * 
	 * @class giant.ui.RichCalendar。Giant UI 的RichCalendar控件。
	 * @example
	 * <pre>
	 * // 将下面代码放入到body内
	 *	&lt;h3&gt;水平不循环滚动&lt;/h3&gt;
	 *	&lt;a href="javascript:void(0);" id="btnLeft1"&gt;上一个&lt;/a&gt;
	 *	&lt;div id="marquee1" class="marquee"&gt;
	 *		&lt;div class="marquee_inner"&gt;&lt;!-- 这一层是必须的 --&gt;
	 *			&lt;div class="itm"&gt;内容1&lt;/div&gt;&lt;div class="itm"&gt;内容2&lt;/div&gt;&lt;div class="itm"&gt;内容3&lt;/div&gt;&lt;div class="itm"&gt;内容4&lt;/div&gt;&lt;div class="itm"&gt;内容5&lt;/div&gt;&lt;div class="itm"&gt;内容6&lt;/div&gt;&lt;div class="itm"&gt;内容7&lt;/div&gt;&lt;div class="itm"&gt;内容8&lt;/div&gt;&lt;div class="itm"&gt;内容9&lt;/div&gt;&lt;div class="itm"&gt;内容10&lt;/div&gt;&lt;div class="itm"&gt;内容11&lt;/div&gt;&lt;div class="itm"&gt;内容12&lt;/div&gt;
	 *		&lt;/div&gt;
	 *	&lt;/div&gt;
	 *	&lt;a href="javascript:void(0);" id="btnRight1"&gt;下一个&lt;/a&gt;
	 * //下面代码放入到head中
	 * var configs1 = {
     *       marquee: '#marquee1',	//跑马灯id
	 *       directionType: giant.ui.Marquee.H, //水平方向滚动 giant.ui.Marquee.H | giant.ui.Marquee.V
	 *       isLoop: true,	//是否循环 true | false 
	 *       step: 110,		//移动步长
	 *       speed: 10,		//移动速度 0-1000整数，0 为不动， 1000最慢
	 *       control: {
	 *       	btnPrev: '#btnLeft1',	//控制滚向上一个
	 *       	btnNext: '#btnRight1',	//控制滚向下一个
	 *       	isAuto: true			//鼠标按下不放，是否自动滚动，true | false
	 *       }
     *   }
     *   $(document).ready(function(){
     *       var marquee1 = new giant.ui.Marquee(configs1);
     *   })
	 * </pre>
	 * @constructor
	 * @param {Object} configs 日历的属性集。
	 */
	giant.ui.Marquee = function(configs){
		this.initMarquee(configs);	
	};
	/**
     * 水平滚动标识
     */
	giant.ui.Marquee.H = 'H';
	/**
     * 垂直滚动标识
     */
	giant.ui.Marquee.V = 'V';

	giant.ui.Marquee.prototype = {
		initMarquee: function(configs){
			var defaultConfigs = {
		        directionType: giant.ui.Marquee.H,
		        isLoop: false,
		        speed: 10,
		        fire: 'mousedown',
		        stopFire: 'mouseup'
			};
			$this = this;
			jQuery.extend(defaultConfigs, configs||{}, true);
			configs = defaultConfigs;
			
			configs.elmMarquee = $(configs.marquee);
			configs.elmMarqueeInner = configs.elmMarquee.find('.marquee_inner');
			configs.btnNext = $(configs.control.btnNext);
			configs.btnPrev = $(configs.control.btnPrev);
			configs.tick = null;	//保存计时器实例
			configs.offer = 0;		//保存要偏移的总距离
			configs.moveDis = 0;	//保存已经偏移的距离
			configs.flag = false; 	//标示鼠标动作， true为down，false为up
			
			if(configs.fire == 'mouseover'){
				configs.isAuto = true;
			}
			
			if(configs.isLoop){
				configs.elmFront = $(configs.elmMarquee[0].innerHTML);
				configs.elmMiddle = $(configs.elmMarquee[0].innerHTML);
				configs.elmBehind = $(configs.elmMarquee[0].innerHTML);
				configs.elmMarqueeInner.html('');
				configs.elmFront.appendTo(configs.elmMarqueeInner);
				configs.elmMiddle.appendTo(configs.elmMarqueeInner);
				configs.elmBehind.appendTo(configs.elmMarqueeInner);
			}
			
			if(configs.directionType == giant.ui.Marquee.H){
				//水平运动
				if(configs.elmMarqueeInner.width() < configs.elmMarquee.width()){
					return;
				}
				if(configs.isLoop){
					//水平循环
					configs.elmFront.css({'float':'left'});
					configs.elmMiddle.css({'float':'left'});
					configs.elmBehind.css({'float':'left'});
					configs.elmMarqueeInner.css({'width':configs.elmMarqueeInner.width()*3});
					configs.offer = configs.elmMiddle.width();
					configs.elmMarqueeInner.css({'margin-left':-configs.offer});
				}
				
				$this.bindMoveLeft(configs);
				$this.bindMoveRight(configs);
				
			} 
			if(configs.directionType == giant.ui.Marquee.V){
				//垂直运动
				if(configs.elmMarqueeInner.height() < configs.elmMarquee.height()){
					return;
				}
				if(configs.isLoop){
					//垂直循环
					configs.elmMarqueeInner.css({'height':configs.elmMarqueeInner.height()*3});
					configs.offer = configs.elmMiddle.height();
					configs.elmMarqueeInner.css({'margin-top':-configs.offer});
				}
				
				$this.bindMoveUp(configs);
				$this.binMoveDown(configs);
			}
			
//			if(configs.control.isAuto){
//				$(document).bind('mouseup',function(event){
//					configs.flag = false;
//				})
//			}
		},
		bindMoveUp: function(configs){
			configs.btnPrev && configs.btnPrev.bind(configs.fire, function(event){
				if(configs.tick != null || (!configs.isLoop && (configs.offer <= 0))){
					return;		
				}
				configs.flag = true;
				configs.elmMiddle = configs.elmMiddle || configs.elmMarqueeInner;
				configs.tick = setInterval(function(){
					if(configs.offer <= 0){
						configs.offer = configs.elmMiddle.height();
						configs.elmMarqueeInner.css({'margin-top':-configs.offer});
					}
					configs.moveDis+=5;
					configs.elmMarqueeInner.css({'margin-top':-configs.offer+configs.moveDis});
					if(configs.moveDis >= configs.step){
						configs.offer -= configs.step;
						configs.moveDis = 0;
						if(configs.isLoop && (!configs.control.isAuto || !configs.flag) || !configs.isLoop && (!configs.control.isAuto || !configs.flag || configs.offer <= 0)){
							clearInterval(configs.tick);
							configs.tick = null;
						}
					}
				},configs.speed);
			});
			configs.btnPrev && configs.btnPrev.bind(configs.stopFire,function(event){
				configs.flag = false;
			});
		},
		binMoveDown: function(configs){
			configs.btnNext && configs.btnNext.bind(configs.fire, function(event){
				if(configs.tick != null || (!configs.isLoop && (configs.elmMarquee.height() + configs.offer >= configs.elmMarqueeInner.height()))){
					return;		
				}
				configs.flag = true;
				configs.elmMiddle = configs.elmMiddle || configs.elmMarqueeInner;
				configs.tick = setInterval(function(){
					if(configs.offer >= 2 * configs.elmMiddle.height()){
						configs.offer = configs.elmMiddle.height();
						configs.elmMarqueeInner.css({'margin-top':-configs.offer});
					}
					configs.moveDis+=5;
					configs.elmMarqueeInner.css({'margin-top':-configs.offer-configs.moveDis});
					if(configs.moveDis >= configs.step){
						configs.offer += configs.step;
						configs.moveDis = 0;
						if((configs.isLoop && (!configs.control.isAuto || !configs.flag)) || (!configs.isLoop && (!configs.control.isAuto || !configs.flag || (configs.elmMarquee.height() + configs.offer >= configs.elmMarqueeInner.height())))){
							clearInterval(configs.tick);
							configs.tick = null;
						} 
					}
				},configs.speed);
			});
			configs.btnNext && configs.btnNext.bind(configs.stopFire,function(event){
				configs.flag = false;
			});
		},
		bindMoveLeft: function(configs){
			configs.btnPrev && configs.btnPrev.bind(configs.fire, function(event){
				if(configs.tick != null || (!configs.isLoop && configs.offer <= 0)){
					return;		
				}
				configs.flag = true;
				configs.elmMiddle = configs.elmMiddle || configs.elmMarqueeInner;
				configs.tick = setInterval(function(){
					if(configs.offer <= 0){
						configs.offer = configs.elmMiddle.width();
						configs.elmMarqueeInner.css({'margin-left':-configs.offer});
					}
					configs.moveDis+=2;
					configs.elmMarqueeInner.css({'margin-left':-configs.offer+configs.moveDis});
					if(configs.moveDis >= configs.step){
						configs.offer -= configs.step;
						configs.moveDis = 0;
						if(configs.isLoop && (!configs.control.isAuto || !configs.flag) || !configs.isLoop && (!configs.control.isAuto || !configs.flag || configs.offer <= 0)){
							clearInterval(configs.tick);
							configs.tick = null;
						}
					}
				},configs.speed);
			});
			configs.btnPrev && configs.btnPrev.bind(configs.stopFire,function(event){
				configs.flag = false;
			});
		},
		bindMoveRight: function(configs){
			configs.btnNext && configs.btnNext.bind(configs.fire, function(event){
				if(configs.tick != null || (!configs.isLoop && (configs.elmMarquee.width() + configs.offer >= configs.elmMarqueeInner.width()))){
					return;		
				}
				configs.flag = true;
				configs.elmMiddle = configs.elmMiddle || configs.elmMarqueeInner;
				configs.tick = setInterval(function(){
					if(configs.offer >= 2 * configs.elmMiddle.width()){
						configs.offer = configs.elmMiddle.width();
						configs.elmMarqueeInner.css({'margin-left':-configs.offer});
					}
					configs.moveDis+=2;
					configs.elmMarqueeInner.css({'margin-left':-configs.offer-configs.moveDis});
					if(configs.moveDis >= configs.step){
						configs.offer += configs.step;
						configs.moveDis = 0;
						if((configs.isLoop && (!configs.control.isAuto || !configs.flag)) || (!configs.isLoop && (!configs.control.isAuto || !configs.flag || (configs.elmMarquee.width() + configs.offer >= configs.elmMarqueeInner.width())))){
							clearInterval(configs.tick);
							configs.tick = null;
						} 
					}
				},configs.speed);
			});
			configs.btnNext && configs.btnNext.bind(configs.stopFire,function(event){
				configs.flag = false;
			});
		}
	};
})(jQuery);